package zhongbo.java.websocket.demo.web.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年03月20日 星期四 02:07:24
 */
@RestController
@Controller
@RequestMapping("/redisTest")
@Api(value = "测试Redis", tags = {"测试Redis相关接口"})
public class RedisTestController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/redis")
    @ApiOperation("测试redis")
    public String redis() {

        String test = redisTemplate.opsForValue().get("test");
        if (StrUtil.isNotBlank(test)) {
            return "redis test ok!";
        }
        return "redis test failed!";
    }

    @GetMapping("/redisExpire")
    @ApiOperation("测试redis key过期时间")
    public String redisTimeout(Integer timeout) {
        // 设置默认值
        if (timeout == null || timeout < 1) {
            timeout = 10;
        }
        redisTemplate.opsForValue().set("user:1", "user_1");
        redisTemplate.expire("user:1", timeout, TimeUnit.SECONDS);
        String user1 = redisTemplate.opsForValue().get("user:1");
        if (user1 != null) {
            return "redis test ok!";
        }
        return "redis test failed!";
    }

    @GetMapping("/redisExpireGet")
    @ApiOperation("测试redis 获取过期key")
    public String redisTimeoutGet() {
        String user1 = redisTemplate.opsForValue().get("user:1");
        if (user1 != null) {
            return "redis test ok!";
        }
        return "redis test failed!";
    }

    @GetMapping("/redisHash")
    @ApiOperation("测试redis hash")
    public String redisHash(String value) {
        if (value == null) {
            value = "default value";
        }
        redisTemplate.opsForHash().put("hash", "testHash", value);
        Object o = redisTemplate.opsForHash().get("hash", "testHash");
        if (o != null) {
            return "redis test ok!";
        }
        return "redis test failed!";
    }

    @GetMapping("/redisHashObj")
    @ApiOperation("测试redis hash")
    public String redisHash() {
        Map<String, String> mapObj = new HashMap<>();
        mapObj.put("test", "999");
        mapObj.put("test1", "9991");
        redisTemplate.opsForHash().putAll("hash:1", mapObj);
        Object o = redisTemplate.opsForHash().get("hash:1", "test");
        if (o != null) {
            return "redis test ok!";
        }
        return "redis test failed!";
    }

    @GetMapping("/redisList")
    @ApiOperation("测试redis list")
    public String redisList() {
        redisTemplate.opsForList().leftPush("list:1", "left");
        String o = redisTemplate.opsForList().leftPop("list:1");
        if (o != null) {
            return "redis test ok!";
        }
        return "redis test failed!";
    }

}
